home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
ISSUE18
/
FONTS
/
DYNFONT.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-01-13
|
3KB
|
108 lines
unit dynfont;
interface
uses SysUtils;
type
{Create a customised exception for this process}
EDynFontError = class(Exception);
procedure LoadFonts(aFontNames : array of string);
procedure DeleteFonts;
implementation
uses {$ifdef WIN32}
windows,
{$else}
wintypes, winprocs,
{$endif}
messages, classes, forms;
var
slFonts : TStringList;
{$ifdef WIN32}
{ Returns the path to the system's TEMP dir (Win32)}
function strTempPath : string;
var
strTempPath : string;
nChars : integer;
begin
SetLength(strTempPath,255); // allocate 255 chars in the string
nChars := GetTempPath(254,PChar(strTempPath)); // get the temp location
// Check that GetTempPath worked ok
if (nChars = 0) or (nChars > 254) then
raise EDynFontError.Create('Can not get location of TEMP directory');
result := strTempPath;
end;
{$endif}
procedure LoadFont(name : string);
var
pstrFotFile, pstrTmp, pzttf : array [0..250] of char;
ttf : string;
begin
{Create a path to this directory & the font file}
ttf := ExtractFilePath(Application.ExeName) + name + '.ttf';
StrPCopy(pzttf,ttf);
{We want to create the .fot files in the temp dir}
{$ifdef WIN32}
GetTempFileName(PChar(strTempPath), PChar(name), 0, pstrFotFile);
{$else}
GetTempFileName(GetTempDrive('x'), StrPCopy(pstrTmp,name), 0, pstrFotFile);
{$endif}
{Now store the temp file name in the string list so we can delete it later}
slFonts.Add(StrPas(pstrFotFile));
{if the fot file exists then delete it}
SysUtils.DeleteFile(StrPas(pstrFotFile));
{Create the fot file}
if not CreateScalableFontResource(1,pstrFotFile,pzttf,nil) then
raise EDynFontError.Create('Error in CreateScaleableFontResource.'+#10+ttf);
{Add it to the font table}
if AddFontResource(pstrFotFile) = 0 then
raise EDynFontError.Create('Error in AddFontResources ' + name);
end;
procedure LoadFonts(aFontNames : array of string);
var
i : integer;
begin
{call loadfont for each item in the array}
for i:=low(aFontNames) to high(aFontNames) do
LoadFont(aFontNames[i]);
{Inform the system that the fonts have changed}
SendMessage($FFFF,WM_FONTCHANGE,0,0);
end;
procedure DeleteFonts;
var
pstrTmp : array [0..150] of char;
i : integer;
begin
{iterate through FOT files}
for i:=0 to slFonts.Count - 1 do
begin
{Remove the font from the window's font table then delete the tmp file}
if not RemoveFontResource(StrPCopy(pstrTmp,slFonts[i])) then
raise EDynFontError.Create('Error in RemoveFontResource');
SysUtils.DeleteFile(slFonts[i]);
end;
{Inform the system that the fonts have changed}
SendMessage($FFFF,WM_FONTCHANGE,0,0);
{$ifndef WIN32}
{Finished with the string list}
slFonts.Free;
{$endif}
end;
initialization
slFonts := TStringList.Create;
{$ifdef WIN32}
finalization
slFonts.Free;
{$endif}
end.